package org.example.controller.user;

import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;

import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/user")
@Controller
@Slf4j
public class userConcroller {
    @Resource
    protected IdentityService identityService;

    //用户列表
    @RequestMapping("/list")
    @ResponseBody
    public List list(HttpServletRequest request) {
        log.info("-------------人员列表-------------");
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        IdentityService identity = processEngine.getIdentityService();
        // Activiti引擎查询用户 一次性查询全部用户
        List<User> list = identity.createUserQuery().list();
        return list;
    }

    @RequestMapping("/add")
    @ResponseBody
    public String add(HttpServletRequest request) {
        log.info("-------------新增人员-------------");
        String id = request.getParameter("id");
        String firstname = request.getParameter("first");
        String lastname = request.getParameter("last");
        String email = request.getParameter("email");
        String password = request.getParameter("pwd");
        // 调用newUser方法创建User实例
        User user = identityService.newUser(id);
        user.setFirstName(firstname);
        user.setLastName(lastname);
        user.setEmail(email);
        user.setPassword(password);
        identityService.saveUser(user);
        // 可以设置用户附加信息 这些信息保存在ACT_ID_INFO表当中
        identityService.setUserInfo(id, "name", firstname.concat(lastname));
        return "add:ok";
    }

    @RequestMapping("/getById")
    @ResponseBody
    public Map getUserById(HttpServletRequest request){
        Map<String,Object> map = new HashMap();
        log.info("-------------查看人员-------------");
        String id = request.getParameter("id");
        // 根据用户id查询用户 当然还可以按照其他条件userEmail().userEmailLike().orderByUserEmail()
        // act_id_user
        User user = identityService.createUserQuery().userId(id).singleResult();
        // 获取UserInfo
        // act_id_info
        String name = identityService.getUserInfo(id, "name");
        System.out.println("userInfo:name = " + name);
        // 获取一个用户的所有UserInfoKeys
        List<String> userInfoKeys = identityService.getUserInfoKeys(id);
        System.out.println("userInfoKeys = " + userInfoKeys);
        // 用于查询指定Id用户所在的组
        List<Group> groupList = identityService.createGroupQuery()
                .groupMember(id).list();
        map.put("user",user);
        map.put("info",userInfoKeys);
        map.put("group",groupList);
        return map;
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public String delete(HttpServletRequest request) {
        String id = request.getParameter("id");
        identityService.deleteUser(id);
        return "delete:ok";
    }

    @RequestMapping("/update")
    @ResponseBody
    public String update(HttpServletRequest request) {
        String id = request.getParameter("id");
        // 调用singleResult方法取出User实例
        User user = identityService.createUserQuery().userId(id).singleResult();
        String firstname = request.getParameter("first");
        String lastname = request.getParameter("last");
        String email = request.getParameter("email");
        String password = request.getParameter("pwd");
        user.setFirstName(firstname);
        user.setLastName(lastname);
        user.setEmail(email);
        user.setPassword(password);
        identityService.saveUser(user);
        return "update:ok";
    }








}
